package com.zb.knowledge.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.knowledge.service.ContVideoService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjVideoService;
import com.zb.util.FileUploadUtil;
import com.zb.utils.TokenUtil;
import com.zb.vo.VideoPathVo;
import com.zb.vo.VideoSelectVo;
import com.zb.vo.VideoVo;
import com.zb.yaml.FilePath;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 产品视频表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-07-31
 */
@Api(tags = "知识中心视频类接口")
@RestController
@RequestMapping("/knowledge/video")
public class ZjVideoController {
    @Resource
    private ZjVideoService videoService;
    @Resource
    private ContVideoService contVideoService;

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "知识中心-视频 - 新增或修改视频")
    @ApiOperation("新增或修改视频")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid VideoVo videoVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        videoService.save(custAccId, videoVo);
        if (null != videoVo.getDelVideoPaths() && !videoVo.getDelVideoPaths().isEmpty()) {
            FileUploadUtil.deleteFileBatchByPath(FilePath.domain(), FilePath.annexVisitPath(), FilePath.videoUploadPath(), videoVo.getDelVideoPaths().stream().map(VideoPathVo::getPath).toList());
        }
        return Result.success(null == videoVo.getId()? "新增视频成功" : "修改视频成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.SELECT, remark = "知识中心-视频 - 获取视频列表")
    @ApiOperation("获取视频列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody VideoSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取视频列表成功").data(videoService.getList(custAccId, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.SELECT, remark = "知识中心-视频 - 获取视频信息")
    @ApiOperation("获取视频信息")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable @ApiParam(value = "视频ID", required = true) Long id) {
        return Result.success("获取视频信息成功").data("row", videoService.getInfo(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "知识中心-视频 - 批量启用或禁用视频")
    @ApiOperation("批量启用或禁用视频")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "视频ID集合", required = true) List<Long> idList) {
        videoService.updateBatchDisableByIds(idList, status);
        return Result.success(status? "禁用视频成功" : "启用视频成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.DELETE, remark = "知识中心-视频 - 批量删除视频")
    @ApiOperation("批量删除视频")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "视频ID集合", required = true) List<Long> idList) {
        contVideoService.deleteByIds(idList);
        return Result.success("删除视频成功");
    }

    @ApiOperation("根据知识库分类ID获取视频")
    @GetMapping("/getVideo/{knowledgeTypeId}")
    public Result getVideo(@PathVariable @ApiParam(value = "知识库分类表ID") Long knowledgeTypeId) {
        return Result.success().data(PageUtil.row(contVideoService.getVideo(knowledgeTypeId)));
    }

}
